class ScriptaculousController < ApplicationController
  # Afficher les livres par odre de préférence
  def bookList
    @books = Book.find(:all, :order => "position")
  end
  
  def list
    @books = Book.find(:all)
  end
  
  def updateTitle
    Book.update(params[:id], :title => params[:value])
    @book = Book.find_by_id(params[:id])
    render :layout => false, :inline => "<%= params[:value] %>" 
  end

  # Réordonner les livres
  def orderBooks
    params[:book_list].each_with_index { |id,idx| 
      Book.update(id, :position => idx) 
    }
    @books = Book.find(:all, :order => "position")
    render :text => 'Ordre mis à jour : ' + @books.join(', ')
  end
  
  def chooseBook    
  end
  
  # Désactiver la protection à cause d'un bogue
  protect_from_forgery :only => [:index]
  
  # Afficher une liste de livres correspondant approximativement à un titre donné
  def auto_complete_for_book_title
     searchedTitle = params[:book][:title]
     @books = Book.find(:all, 
        :conditions => [ 'LOWER(title) LIKE ?', '%' + searchedTitle.downcase + '%' ], 
        :order => 'title ASC',
        :limit => 8)
     render :partial => 'bookList'
  end
end
